/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DAO.DataProvider;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author QuangHau
 */
public class ConnectionDatabase extends javax.swing.JDialog {

    /**
     * Creates new form ConnectionDatabase
     */
    private boolean Exit;
    public boolean  isreset = false;

    public ConnectionDatabase(java.awt.Frame parent, boolean modal, boolean exit) {
        super(parent, modal);
        this.Exit = exit;
        initComponents();
        try (BufferedReader br = new BufferedReader(new FileReader("Database.txt"))) {
            txtHost.setText(br.readLine());// host
            txtPort.setText(br.readLine());
            txtDatabase.setText(br.readLine());
            txtUser.setText(br.readLine());
            txtPassword.setText(br.readLine());
            br.close();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lbUser = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lbPasswor = new javax.swing.JLabel();
        txtHost = new javax.swing.JTextField();
        txtPort = new javax.swing.JTextField();
        txtDatabase = new javax.swing.JTextField();
        txtUser = new javax.swing.JTextField();
        btnKetNoi = new javax.swing.JButton();
        btnHuy = new javax.swing.JButton();
        txtPassword = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Kết nối cơ sở dữ liệu");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Host:");

        jLabel2.setText("Database Name:");

        lbUser.setText("User Name:");

        jLabel4.setText("Port:");

        lbPasswor.setText("Password:");

        btnKetNoi.setText("Kết nối");
        btnKetNoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKetNoiActionPerformed(evt);
            }
        });

        btnHuy.setText("Hủy");
        btnHuy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHuyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2)
                            .addComponent(lbUser)
                            .addComponent(lbPasswor))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtHost)
                            .addComponent(txtPort)
                            .addComponent(txtDatabase)
                            .addComponent(txtUser)
                            .addComponent(txtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(108, 108, 108)
                        .addComponent(btnKetNoi)
                        .addGap(26, 26, 26)
                        .addComponent(btnHuy)))
                .addContainerGap(60, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnHuy, btnKetNoi});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDatabase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbUser))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbPasswor)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnKetNoi)
                    .addComponent(btnHuy))
                .addContainerGap(32, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnHuyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHuyActionPerformed
        if (Exit) {
            System.exit(0);
        }
        this.dispose();
    }//GEN-LAST:event_btnHuyActionPerformed

    private void btnKetNoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnKetNoiActionPerformed
        String err = "";
        JTextField textField = null;
        if (txtPassword.getText().equals("")) {
            err = "Password: không được để trống\n" + err;
            textField = txtPassword;
        }

        if (txtUser.getText().equals("")) {
            err = "User: không được để trống\n" + err;
            textField = txtUser;
        }

        if (txtDatabase.getText().equals("")) {
            err = "Database Name: không được để trống\n" + err;
            textField = txtDatabase;
        }
        if (txtPort.getText().equals("")) {
            err = "Port: không được để trống\n" + err;
            textField = txtPort;
        }

        if (txtHost.getText().equals("")) {
            err = "Host: không được để trống\n" + err;
            textField = txtHost;

        }

        if (!err.equals("")) {
            JOptionPane.showMessageDialog(null, err, "Lỗi", JOptionPane.ERROR_MESSAGE);
            textField.requestFocus();
            return;
        }

        String constr = "jdbc:sqlserver://" + txtHost.getText();// host
        constr += ":" + txtPort.getText();
        constr += ";databaseName=" + txtDatabase.getText();
        constr += ";user=" + txtUser.getText();
        constr += ";password=" + txtPassword.getText();
        DataProvider.connectionString = constr;
        if (!DataProvider.Successful()) {
            JOptionPane.showMessageDialog(null, "Không thể kết nối cơ sở dữ liệu", "Lỗi", JOptionPane.ERROR_MESSAGE);
        } else {
            isreset = true;
            try (FileWriter fw = new FileWriter("Database.txt")) {
                String str = txtHost.getText() + "\r\n";// host
                fw.write(str);

                str = txtPort.getText() + "\r\n";// port
                fw.write(str);

                str = txtDatabase.getText() + "\r\n";// Database
                fw.write(str);

                str = txtUser.getText() + "\r\n";// user
                fw.write(str);

                str = txtPassword.getText() + "\r\n";// password
                fw.write(str);

                fw.close();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
            JOptionPane.showMessageDialog(null, "Kết nối thành công", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        }

    }//GEN-LAST:event_btnKetNoiActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        if (Exit) {
            System.exit(0);
        }
        this.dispose();
    }//GEN-LAST:event_formWindowClosing
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHuy;
    private javax.swing.JButton btnKetNoi;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel lbPasswor;
    private javax.swing.JLabel lbUser;
    private javax.swing.JTextField txtDatabase;
    private javax.swing.JTextField txtHost;
    private javax.swing.JTextField txtPassword;
    private javax.swing.JTextField txtPort;
    private javax.swing.JTextField txtUser;
    // End of variables declaration//GEN-END:variables
}
